<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="query">
                <el-form-item>
                    <el-input v-model="query.keyword" placeholder="关键字"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="search">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="pageInfo.rows" highlight-current-row v-loading="this.listLoading" @selection-change="selsChange"
                  style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="username" label="员工名" width="120">
            </el-table-column>

            <el-table-column prop="email" label="邮箱" width="250">
            </el-table-column>


            <el-table-column prop="age" label="年龄" width="120">
            </el-table-column>

            <el-table-column prop="role.name" label="角色名称" min-width="180">
            </el-table-column>

            <el-table-column prop="department.name" label="所属部门名称" min-width="180">
            </el-table-column>


            <el-table-column label="操作" width="300">
                <template scope="scope">
                    <el-button type="success" size="small" @click="handleRole(scope.$index, scope.row)">设置角色
                    </el-button>
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--批量删除-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10"
                           :current-page="query.currentPage"
                           :total="pageInfo.total" style="float:right;">
            </el-pagination>
        </el-col>

        <!--编辑/新增界面-->
        <!--高版本的要用 :visible.sync="saveFormVisible"-->
        <el-dialog title="编辑/新增" :visible.sync="saveFormVisible" :close-on-click-modal="false">
            <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
                <el-form-item label="用户名称">
                    <el-input v-model="saveForm.username" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="邮箱">
                    <el-input v-model="saveForm.email"></el-input>
                </el-form-item>

                <el-form-item label="年龄">
                    <el-input v-model="saveForm.age"></el-input>
                </el-form-item>
                <el-form-item label="部门">
                    <el-select v-model="saveForm.departmentId" placeholder="请选择">
                        <el-option
                                v-for="item in department"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="saveFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
            </div>
        </el-dialog>

        <el-dialog title="设置角色" :visible.sync="saveRoleVisible" :close-on-click-modal="false">
            <el-form :model="roleForm" label-width="80px" ref="saveForm">

                <el-form-item label="角色">
                    <el-select v-model="roleForm.role.id" placeholder="请选择">
                        <el-option
                            v-for="item in role"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="saveRoleVisible = false">取消</el-button>
                <el-button type="primary" @click.native="saveRole" :loading="saveLoading">提交</el-button>
            </div>
        </el-dialog>

    </section>
</template>

<script>

export default {
    data() {
        return {
            employees: [],
            department: [],
            role:[],
            pageInfo: {
                rows: [],
                total: 0
            },

            sels: [],//列表选中列

            saveFormRules: {
                name: [
                    {required: true, message: '请输入姓名', trigger: 'blur'}
                ]
            },
            //编辑界面数据
            saveForm: {
                id: null,
                username: '',
                email: '',
                age: '',
                departmentId:'',
                department:{
                    id:'',
                  name:''
                }
            },

            roleForm:{
                id:'',
                role:{
                    id:''
                }
            },
            saveLoading: false,
            addLoading: false,
            saveFormVisible: false,//界面是否显示

            saveRoleVisible:false,
            query: {
                currentPage: 1,
                PageSize: 10,
                keyword: ""
            },
            listLoading:false,
        }
    },
    methods: {
        saveRole(){

            this.$confirm('要确定提交吗？', '提示', {}).then(() => {


                this.$http.post("/employee/setRole", this.roleForm)
                    .then(result => {
                        result = result.data;

                        if (result.success) {
                            this.$message({
                                message: '成功',
                                type: 'success'
                            });
                        }

                        this. saveRoleVisible = false;
                        this.getEmployees();
                    })
                    .catch(result => {
                        this.$message({
                            message: '网络错误',
                            type: 'error'
                        });
                    });
            })
        },
        saveSubmit() {
            this.$confirm('要确定提交吗？', '提示', {}).then(() => {
                this.addLoading = true;

                this.$http.put("/employee", this.saveForm)
                    .then(result => {
                        result = result.data;

                        if (result.success) {
                            this.$message({
                                message: '成功',
                                type: 'success'
                            });
                        }
                        this.saveFormVisible = false;
                        this.getEmployees();
                    })
                    .catch(result => {
                        this.$message({
                            message: '网络错误',
                            type: 'error'
                        });
                    });
            })
        },
        batchRemove() {
            var ids = this.sels.map(item => item.id);
            this.$confirm('确认删除该记录吗?', '提示', {
                type: 'warning'
            }).then(() => {//确认删除的按钮
                //加载页面设为true
                this.listLoading = true;

                //调用批量删除
                this.$http.patch("/employee/", ids)
                    .then(result => {
                        //关闭加载框
                        this.listLoading = false;
                        //重新查询数据
                        this.search()
                        result = result.data
                        if (result.success) {
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });

                        } else {
                            this.$message({
                                message: '删除成功',
                                type: 'error'
                            });
                        }
                    })
                    .catch(result => {
                        this.$message({
                            message: '网络错误',
                            type: 'error'
                        });

                    }).catch(() => {//取消删除回调函数
                    this.$message('已取消删除');
                })
            })
        },
        handleDel(index, row) {

            this.$confirm('确认删除该记录吗?', '提示', {
                type: 'warning'
            }).then(() => {//确认删除的按钮
                //加载页面设为true
                this.listLoading = true;
                //获取删除数据id
                let id = row.id;

                //发送请求 "/dept/3"
                this.$http.delete("/employee/" + id)
                    .then(result => {
                        //关闭加载框
                        this.listLoading = false;
                        //重新查询数据
                        this.search()
                        result = result.data
                        if (result.success) {
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });

                        } else {
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                        }
                    })
            })
        },
        search() {
            this.query.currentPage = 1;
            this.getEmployees()
        },
        //点击页码的方法
        handleCurrentChange(val) {
            this.query.currentPage = val;
            this.getEmployees()
        },

        getEmployees() {

            this.$http.post("/employee", this.query)
                .then(result => {
                    result = result.data;
                    this.pageInfo = result.data


                }).catch(result => {
                this.$message({
                    message: '网络错误',
                    type: 'error'
                });
            })
        },



        handleRole: function (index, row){
            this.saveRoleVisible = true;
            this.roleForm = Object.assign({}, row);
            if(this.roleForm.role == null){
                this.roleForm = {id:row.id,role:{id:''}}
            }


            this.$http.get("/role")
                .then(result => {
                    result = result.data;
                    this.role = result.data;
                })

        },






        handleEdit: function (index, row) {
            this.saveFormVisible = true;
            this.saveForm = Object.assign({}, row);

            this.$http.get("/department")
                .then(result => {
                    result = result.data;
                    this.department = result.data;

                })


        },
        //显示新增界面
        handleAdd: function () {

            this.saveFormVisible = true;
            //每次新增都要将表单数据初始化
            this.saveForm = {
                id: null,
                username: '',
                email: '',
                age: '',
                departmentId: '',
                department:{
                    name:''
                }
            };
            this.$http.get("/department")
                .then(result => {
                    result = result.data;
                    this.department = result.data;

                })

            this.getAllEmployee();

        },
        getAllEmployee: function () {
            this.$http.get("/employee")
                .then(result => {
                    result = result.data;
                    this.employees = result.data;

                })
        },

        selsChange: function (sels) {
            this.sels = sels;
        },

    },
    mounted() {
        this.getEmployees();
    }
}

</script>

<style scoped>

</style>